//
//  LXShoppingCarCommodityViewCell.m
//  绿稀全球购
//
//  Created by 诸葛孔明 on 16/6/2.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXShoppingCarCommodityViewCell.h"

@interface LXShoppingCarCommodityViewCell()

/**
 *  商品图片
 */
@property(nonatomic,strong)UIImageView * commodityImgv;

/**
 *  商品名字
 */
@property(nonatomic,strong)UILabel * commodityNameLabel;

/**
 *  商品描述
 */
@property(nonatomic,strong)UILabel * commodityDescLabel;


/**
 *  商品价格
 */
@property(nonatomic,strong)UILabel * commodityPriceLabel;

/**
 *  步进器
 */
//@property(nonatomic,strong)LXSteper * steper;



@property(nonatomic,strong)UIView * editingView;

@property(nonatomic,assign)BOOL isShoweditingView;

@end

@implementation LXShoppingCarCommodityViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.isShoweditingView = NO;
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.backgroundColor = [UIColor whiteColor];
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.isShoweditingView = NO;
        if(ScreenSize.width>320){
        self.cellHeight = 130;
        }
        else{
            self.cellHeight = 100;
        }
        
        LXSteper * steper = [[LXSteper alloc] initWithFrame:CGRectMake(0, 0, 80, 25)];
        self.steper = steper;
        [self.contentView addSubview:steper];
        
        steper.addCallBack = ^(LXSteper * steper)
        {
            if(self.delegate&&[self.delegate respondsToSelector:@selector(LXShoppingCarCommodityViewCell:didClickAddSteper:)]){
                [self.delegate LXShoppingCarCommodityViewCell:self didClickAddSteper:self.steper];
            }
        };
        
        steper.reduceCallBack = ^(LXSteper * steper)
        {
            if(self.delegate&&[self.delegate respondsToSelector:@selector(LXShoppingCarCommodityViewCell:didClickReduceSteper:)]){
                [self.delegate LXShoppingCarCommodityViewCell:self didClickReduceSteper:self.steper];
            }

        };
        
        
        
        UIButton * selectedButton = [[UIButton alloc] init];
        [selectedButton setImage:[UIImage imageNamed:@"shoppingcart_but_n"] forState:UIControlStateNormal];
        [selectedButton setImage:[UIImage imageNamed:@"shoppingcart_but_s"] forState:UIControlStateSelected];
        
        [selectedButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:selectedButton];
        self.selectedBtn = selectedButton;
        
        
        UIImageView * commodityImgv = [[UIImageView alloc] init];

        [self.contentView addSubview:commodityImgv];
        self.commodityImgv = commodityImgv;
        
        UILabel * commodityNameLabel = [[UILabel alloc] init];
        commodityNameLabel.numberOfLines = 0;
        commodityNameLabel.textAlignment = NSTextAlignmentLeft;
        commodityNameLabel.font = [UIFont systemFontOfSize:11];
        commodityNameLabel.textColor = [UIColor blackColor];

        [self.contentView addSubview:commodityNameLabel];
        self.commodityNameLabel = commodityNameLabel;
        
        
        UILabel * commodityDescLabel = [[UILabel alloc] init];
        commodityDescLabel.textAlignment = NSTextAlignmentLeft;
        commodityDescLabel.font = [UIFont systemFontOfSize:14];
        commodityDescLabel.textColor = [UIColor RGBColor:117 green:119 blue:130];

        [self.contentView addSubview:commodityDescLabel];
        self.commodityDescLabel = commodityDescLabel;
        
        
        UILabel * commodityPriceLabel = [[UILabel alloc] init];
        commodityPriceLabel.numberOfLines = 0;
        commodityPriceLabel.textAlignment = NSTextAlignmentLeft;
        commodityPriceLabel.font = [UIFont systemFontOfSize:16];
        commodityPriceLabel.textColor = [UIColor RGBColor:235 green:0 blue:0];
        [self.contentView addSubview:commodityPriceLabel];
        self.commodityPriceLabel = commodityPriceLabel;
        
        UIView * underLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.cellHeight-0.5, ScreenSize.width, 0.5)];
        underLine.backgroundColor = HexColor(@"#cccccc");
        [self.contentView addSubview:underLine];

    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.contentView.width = ScreenWidth;
    self.selectedBtn .centerY = self.cellHeight/2.0;
    self.selectedBtn.x = 10;
    self.selectedBtn.width = 30;
    self.selectedBtn.height = 30;
    
    self.steper.x = (ScreenSize.width-self.steper.width)-globalMargin;
    self.steper.botton = self.cellHeight-globalMargin;
    
    
    self.commodityImgv.centerY = self.selectedBtn.centerY;
    self.commodityImgv.x = self.selectedBtn.right+5;
    self.commodityImgv.height = self.cellHeight*0.8;
    self.commodityImgv.width = self.commodityImgv.height;
    
    self.commodityNameLabel.height = self.commodityImgv.height*0.35;
    self.commodityNameLabel.width = ScreenSize.width - self.commodityImgv.right-10;
    self.commodityNameLabel.x = self.commodityImgv.right + 5;
    self.commodityNameLabel.y = self.commodityImgv.y;
    
    self.commodityDescLabel.width = self.commodityNameLabel.width;
    self.commodityDescLabel.height = self.commodityImgv.height * 0.2;
    self.commodityDescLabel.y = self.commodityNameLabel.botton;
    self.commodityDescLabel.x = self.commodityNameLabel.x;
    
    self.commodityPriceLabel.width = [self.commodityPriceLabel.text sizeInAre:MaxSize font:self.commodityPriceLabel.font].width;
    self.commodityPriceLabel.height = [self.commodityPriceLabel.text sizeInAre:MaxSize font:self.commodityPriceLabel.font].height;;
    self.commodityPriceLabel.x =self.commodityDescLabel.x;
    self.commodityPriceLabel.botton = self.commodityImgv.botton;
    
    self.editingView.size = CGSizeMake(100, self.cellHeight);
    self.editingView.x = ScreenWidth - self.editingView.width;
}

-(void)buttonClick:(UIButton *)button{
    self.selectedBtn.selected = !self.selectedBtn.selected;
    if(self.delegate&&[self.delegate respondsToSelector:@selector(LXShoppingCarCommodityViewCell:product:isSelected:)]){
        [self.delegate LXShoppingCarCommodityViewCell:self product:self.product isSelected:self.selectedBtn.selected];
    }
}

-(void)setProduct:(Product *)product{
    _product = product;
    self.steper.steperValue = _product.numberInShopCart;
    [self.commodityImgv sd_setImageWithURL:[NSURL URLWithString:_product.small_pic_wap] placeholderImage:nil];
    self.commodityNameLabel.text = _product.name;
    self.commodityDescLabel.text = _product.subname;
    self.commodityPriceLabel.text = _product.specialprice;
    [self setNeedsLayout];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    
}



@end
